<?php

namespace app\api\model;

use think\Model;
use think\model\concern\SoftDelete;

/**
 * 商品列表
 */
class Goods extends Model
{
    // protected static function base($query)
    // {
    //     $query->where('status',1);
    // }

    protected $name='mall_goods';

    // 开启自动写入时间戳字段
    protected $auto_timestamp = 'false';

    // 设置json类型字段
	// protected $json = ['sf'];
 
      // 追加属性

      use SoftDelete;

      protected $deleteTime = 'delete_time';

      protected $append = [
        'status_text',
        'img_url_arr',
        'sf'
    ];

   
 
    public function getTypeTextAttr($value)
    {
      
        $status = [-1=>'删除',0=>'禁用',1=>'正常',2=>'待审核'];
        return '234324234';
    }

 

    public function getStatusTextAttr($value)
    {
        $status = [-1=>'删除',0=>'禁用',1=>'正常',2=>'待审核'];
        return 'test';
    }

    public function getImgUrlArrAttr($data,$value)
    { 
        $url = $value['images'];
        $url= explode('|',$url);
        return $url;
    }

    //处理收费标准
    public function getsfAttr($value, $data){
        $goodsProduct = new GoodsProduct();
        $sf_info = $goodsProduct->where('gid', $data['id'])->select();
        if(!empty($sf_info)){
            $ret = [];
            foreach($sf_info as $key1 => $v) {
                $sf_info_temp = json_decode($v['sf'],true);
                // $new_info = array();
                // var_dump($sf_info_temp);exit;
                foreach($sf_info_temp as $key=>$value){
                    $body = [];
                    // var_dump($value[]);exit;
                   if (isset($value['type_name']) && !empty($value['type_name'])) {
                        for($i = 0 ; $i < count($value['type_name']);$i++){
                            $body[$i]['type_name']=$value['type_name'][$i];
                            $body[$i]['value']=$value['value'][$i];
                            // $body[$i]=[$value['type_name'][$i]=>$value['value'][$i]];
                            // var_dump($body);exit;
                        }
                   }
                   if (!empty($body)){
                     $sf_info_temp[$key]['body']=$body;
                   } else {
                    //   $sf_info_temp[$key]['body']=[];
                   }
                    
                }
                // array_push($ret, $)
                $sf_info[$key1]['sf'] = $sf_info_temp;
            }
        }
        return $sf_info;
    }

    

}
